//
//  PaperListViewModel.swift
//  Exam-iOS
//
//  Created by 李胜锋 on 2018/3/6.
//  Copyright © 2018年 李胜锋. All rights reserved.
//

import Foundation
import RxCocoa
import RxSwift
import RxDataSources
import Moya

class PaperListViewModel: LSFRefreshProtocol {
    
    //RX 垃圾回收袋
    var disposeBag = DisposeBag()
    
    //参数
    let levelId = AccountStorage.default.account.levelId ?? 1
    var pageNum: Int = 1
    var classId: Int!
    
    public let vmDatas  = Variable([PaperListSection]())
    
    ///刷新状态
    var refreshStatus = Variable<LSFRefreshStatus>(.none)
    
    ///是否是空数据
    var isNull = Variable<Bool>(false)
    
    /// 发送请求，参数：是否是下拉刷新
    var requestCommand = PublishSubject<Bool>()
    
    
    init() {
        self.requestCommand.subscribe(onNext: { (isPull) in
            if isPull {
                self.pageNum = 1
            }
            self.paperList()
        }).disposed(by: disposeBag)
    }
    
    
    func paperList() {
        
        paperProvider.rx
            .request(.paperList(levelId: levelId, classId: classId, pageNum: pageNum))
            .asObservable()
            .mapRespnseJson()
            .subscribe(onNext: { [weak self] (paperList) in
                guard let strongSelf = self else { return }
                guard let list = paperList as? [[String: Any]] else {
                    return
                }
                
                
                var sections = [PaperListSection]()
                for object in list {
                    let title = object["title"] as? String
                    let papers = object["papers"]
                    if let item = PaperListModel.array(papers) {
                        let section = PaperListSection.init(header: title, items: item)
                        sections.append(section)
                    }
                }
                
                if strongSelf.pageNum == 1 {
                    strongSelf.vmDatas.value.removeAll()
                }
                if sections.count > 0 {
                    strongSelf.pageNum += 1
                    strongSelf.vmDatas.value.append(contentsOf: sections)
                }
                
                }, onError: { [weak self] (error) in
                    stopAnimating()
                    self?.refreshStatus.value = .endHeaderRefresh
                    self?.refreshStatus.value = .endFooterRefresh
            }, onCompleted: { [weak self] in
                stopAnimating()
                self?.isNull.value = (self?.vmDatas.value.count == 0)
                self?.refreshStatus.value = .endHeaderRefresh
                self?.refreshStatus.value = .endFooterRefresh
            }).disposed(by: disposeBag)
        
    }
}


/* ============================= 自定义section =============================== */

struct PaperListSection {
    var header: String?
    var items: [Item]
}

extension PaperListSection: SectionModelType {
    typealias Item = PaperListModel
    
    init(original: PaperListSection, items: [PaperListModel]) {
        self = original
        self.items = items
    }
}
